#include "attribute_handle.h"

/*用于解析串口下发的高速数据*/
void parse_uart_play_data(u16 len,u8 *g_uart_buf)
{
    u16 color_length = (len/3)*4+8;
    u16 light_length= len/3;
    printf("len is %d,color_length %d\n",len,color_length);

    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *)); //给结构体分配空间

    if(spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create spi_param err\n");
        return;
    }

    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*color_length); //给数据传输分配空间
    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create uart_rx_buffer err\n");
        return;
    }

    uart_rx_buffer[0]=0x30;
    uart_rx_buffer[1]=0x30;
    uart_rx_buffer[2]=0x30;
    uart_rx_buffer[3]=0x30;
    uart_rx_buffer[4]=~0x30;
    uart_rx_buffer[5]=~0x30;
    uart_rx_buffer[6]=~0x30;
    uart_rx_buffer[7]=~0x30;

    for (int j = 0; j <= light_length; j += 1)  //算出每个灯珠的颜色
    { 
        uart_rx_buffer[j*4+8+0] = 0; //W
        uart_rx_buffer[j*4+8+1] = g_uart_buf[j*3+0]; //R
        uart_rx_buffer[j*4+8+2] = g_uart_buf[j*3+1]; //G
        uart_rx_buffer[j*4+8+3] = g_uart_buf[j*3+2]; //B
    }

    spi_param->send_data = uart_rx_buffer;
    spi_param->send_len= color_length;
    uart_rx_buffer = NULL;
    xQueueSend(spi_queue_handle,&spi_param,0);
}

void attribute_handle_data(u8 *data,u16 len,DATA_SOURCE source)
{   
    for(int i=20;i<len;) //for循环缺省判断
    {
        u32 target=0;
        target |= data[i+0];
        target |= data[i+1] << 8;
        target |= data[i+2] << 16;
        target |= data[i+3] << 24;
        printf("i is %lx\n",target);

        switch (target)
        {
            case HIGH_SPEED: //高速模式包
                u16 num1 = ((uint16_t)data[i+4] | ((uint16_t)data[i+4+1] << 8));
                parse_uart_play_data(num1,&data[i+4+2]);
                i+=4;
                i+=(num1+2);
                break;

            case REGISTER: //重发注册包
                send_register_package();
                i+=4;
                break;

            case INQUIRE: //查询指令
                iot_report_init();
                i +=4;
                break;
            
            case RGB_BRIGHNESS:  //RGB灯的亮度
                u8 num2 = data[i+4]; //开关赋值
                RGB_brighness_handle(num2);
                i +=(4+1);
                LOG_INFO("set RGB brighness is%d,i is %d\n",num2,i);
                break;            
            
            case OPEN_CLOSE:
                u8 num15 = data[i+4];
                if(num15 ==0x01)
                {
                    app_logic_set_ssp(SSP_POWER_OFF);
                }
                else if(num15 == 0x02)
                {
                    app_logic_set_ssp(app_logic_get_recover_ssp());
                }
                i+=(4+1);
                LOG_INFO("set OPEN_CLOSE is%d,i is %d\n",num15,i);
                break;

            case MAIN_MODE: //主模式切换
                u8 num4 = data[i+4]; //主模式值
                main_mode_handle(num4);
                i+=(4+1);
                LOG_INFO("set main mode is%d,i is %d\n",num4,i);
                break;

            case SCENE_MODE: //场景模式
                u8 num10 = data[i+4];
                set_scene_ssp(num10);
                LOG_INFO("SCENE_MODE %d\n",num10);
                i+=(4+1);
                break;  

            case EFFECT_SSP: //自定义灯光效果
                u8 num11 = data[i+4];
                set_light_effect_enum(num11);
                LOG_INFO("EFFECT_SSP %d\n",num11);
                i+=(4+1);
                break;
            
            case CUSTOM_SSP: //自定义工作模式
                u8 num12 = data[i+4];
                set_custom_ssp(num12);
                LOG_INFO("CUSTOM_SSP %d\n",num12);
                i+=(4+1);
                break;

            case CUSTOM_WAIT:  //自定义延时时间
                u8 num13 = data[i+4];
                set_light_wait(num13);
                LOG_INFO("CUSTOM_WAIT %d\n",num13);
                i+=(4+1);
                break;

            case CUSTOM_PART_DATA: //分段信息
                light_effect_config.len = data[i+4];
                LOG_INFO("light_effect_config.len %d\n",light_effect_config.len);
                memset(light_effect_config.buf,0,45);
                memcpy(light_effect_config.buf,&data[i+5],light_effect_config.len);
                i+=(4+1+light_effect_config.len);
                break;

            case CUSTOM_SAVE: //自定义保存模式
                u8 num14 = data[i+4];
                custom_write_partition(num14,&data[i+5]);
                custom_save_report();
                set_custom_ssp(num14);
                LOG_INFO("CUSTOM_SAVE %d\n",num14);
                i+=(4+1+48);
                break;            

            case NET_RESET: //重置网络信息
                wifi_logic_set_ssp(BULFI_OPEN);
                net_reset_report();
                LOG_INFO("NET_RESET\n");
                i+=4;
                break;

            case OTA:
                u16 num6 = (u16)(data[i+4+1]|data[i+4]);
                memcpy(ota_data,&data[i+4+2],num6);
                ota_data[num6] ='\0';
                printf("num6 %d\n",num6);
                printf("ota_data %s\n",ota_data);
                init_http_ota();
                i+=(4+2+2+num6);
                break;

            default:
            LOG_INFO("Instruction number parsing error\n");
            i+=1024;
                break;
        }

    }
    LOG_INFO("attribute over\n");
}